//
//  UINavigationItem+Custom.m
//  ITV
//
//  Created by inphase on 17/4/1.
//  Copyright © 2017年 inphase. All rights reserved.
//

#import "UINavigationItem+Custom.h"
#import <objc/runtime.h>

@implementation UINavigationItem (Custom)

+(void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method originalMethodImp = class_getInstanceMethod(self, @selector(leftBarButtonItem));
        Method destMethodImp = class_getInstanceMethod(self, @selector(myCustomBackButton));
        method_exchangeImplementations(originalMethodImp, destMethodImp);

    });
}

static char kCustomBackButtonKey;

-(UIBarButtonItem *)myCustomBackButton{
//    UIBarButtonItem *item = [self myCustomBackButton];
//    if (item) {
//        return item;
//    }
    UIBarButtonItem *item = objc_getAssociatedObject(self, &kCustomBackButtonKey);
    if (!item) {
        item = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:NULL];
        objc_setAssociatedObject(self, &kCustomBackButtonKey, item, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return item;
}

- (void)dealloc
{
    objc_removeAssociatedObjects(self);
}

@end
